from src.main.server.mapper.CreateMysqlCon import CreateMysqlCon


class ServerInfoImpl:

    def __init__(self):
        self.con = CreateMysqlCon().get_con()
        if not self.con:
            raise ConnectionError("数据库连接失败，请检查配置或网络")
        self.cursor = self.con.cursor()

    def all_info(self):
        """查询所有服务项目信息"""
        SQL = ("SELECT server_name, place, server_info, server_start, server_end, request_person, current_person "
               "FROM server_project ")
        self.cursor.execute(SQL)
        return self.cursor.fetchall()

    def add(self, obj):
        SQL = ("INSERT INTO server_project "
               "(server_name, place, server_info, server_start, server_end, request_person) "
               "VALUES "
               "(%s, %s, %s, %s, %s, %s)")
        self.cursor.execute(SQL, obj)

    def get_info(self, fd, sd):
        """查询回显"""
        SQL = ("select server_name, place, server_info, server_start, server_end, request_person "
               "from server_project "
               "where server_name = %s and place = %s")
        self.cursor.execute(SQL, (fd, sd))
        return self.cursor.fetchall()

    def update(self, data):

        SQL = ("update server_project "
               "set server_name = %s, "
               "    place = %s,"
               "    server_info = %s,"
               "    server_start = %s,"
               "    server_end = %s,"
               "    request_person = %s "
               "where server_name = %s and place = %s")
        self.cursor.execute(SQL, data)

    def delete(self, fd, sd):
        SQL = ("delete from server_project "
               "where server_name = %s and place = %s")
        self.cursor.execute(SQL,(fd, sd))

    def addCount(self,data):
        SQL = ("update server_project "
               "set current_person = current_person + 1 "
               "where server_name = %s")
        self.cursor.execute(SQL, data)

    def check(self, data):
        SQL = ("select request_person, current_person "
               "from server_project "
               "where server_name = %s")
        self.cursor.execute(SQL, data)
        return self.cursor.fetchall()


if __name__ == '__main__':
    print(ServerInfoImpl().get_info("环保活动", "公园A"))